package pixelradar.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.fml.client.config.GuiButtonExt;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.GuiConfigEntries;
import net.minecraftforge.fml.client.config.GuiConfigEntries.CategoryEntry;
import net.minecraftforge.fml.client.config.GuiConfigEntries.IConfigEntry;
import net.minecraftforge.fml.client.config.IConfigElement;
import pixelradar.Mw;
import pixelradar.config.LargeMapModeConfig;
import pixelradar.config.MapModeConfig;
import pixelradar.config.SmallMapModeConfig;
import pixelradar.map.MapRenderer;
import pixelradar.map.mapmode.MapMode;

import java.awt.geom.Point2D.Double;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class ModGuiConfigHUD extends GuiConfig {
    public MapMode mapMode;
    protected GuiButtonExt btnTopLeft;
    protected GuiButtonExt btnTopRight;
    protected GuiButtonExt btnBottomLeft;
    protected GuiButtonExt btnBottomRight;
    protected GuiButtonExt btnCenterTop;
    protected GuiButtonExt btnCenterBottom;
    protected GuiButtonExt btnCenterLeft;
    protected GuiButtonExt btnCenterRight;
    protected GuiButtonExt btnCenter;
    private Mw mw;
    private MapRenderer map;
    private Boolean DraggingMap;
    private MapModeConfig dummyMapConfig;

    public ModGuiConfigHUD(GuiScreen parentScreen, List<IConfigElement> configElements, String modID, String configID, boolean allRequireWorldRestart, boolean allRequireMcRestart, String title, String Config) {
        super(parentScreen, configElements, modID, configID, allRequireWorldRestart, allRequireMcRestart, title, "Use right click and hold to move the pixelradar.map");
        this.DraggingMap = Boolean.FALSE;
        if ("fullscreenmap".equals(Config)) {
            this.dummyMapConfig = new MapModeConfig("fullscreenmap");
        } else if ("largemap".equals(Config)) {
            this.dummyMapConfig = new LargeMapModeConfig("largemap");
        } else if ("smallmap".equals(Config)) {
            this.dummyMapConfig = new SmallMapModeConfig("smallmap");
        }

        this.dummyMapConfig.setDefaults();
        this.dummyMapConfig.loadConfig();
        this.mw = Mw.getInstance();
        this.mapMode = new MapMode(this.dummyMapConfig);
        this.map = new MapRenderer(this.mw, this.mapMode, null);
    }

    public void initGui() {
        super.initGui();
        int topLeftWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.topleft")) + 20, 100);
        int topRightWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.topright")) + 20, 100);
        int bottomLeftWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.botleft")) + 20, 100);
        int bottomRightWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.botright")) + 20, 100);
        int centerTopWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.centertop")) + 20, 100);
        int centerBottomWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.centerbottom")) + 20, 100);
        int centerWidth = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.center")) + 20, 100);
        int centerLeft = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.centerleft")) + 20, 100);
        int centerRight = Math.max(this.mc.fontRenderer.getStringWidth(I18n.format("mw.config.map.ctgy.position.centerright")) + 20, 100);
        int buttonWidthHalf1 = (bottomLeftWidth + 5 + bottomRightWidth + centerTopWidth + 5) / 2;
        int buttonWidthHalf2 = (centerLeft + 5 + centerWidth + centerRight + 5) / 2;
        int buttonWidthHalf3 = (topLeftWidth + 5 + topRightWidth + centerBottomWidth + 5) / 2;
        int buttonHeigth1 = this.height - 29 - 29 - 29 - 29;
        int buttonHeigth2 = this.height - 29 - 29 - 29;
        int buttonHeigth3 = this.height - 29 - 29;
        this.buttonList.add(new GuiButtonExt(3000, this.width / 2 - buttonWidthHalf1, buttonHeigth1, topLeftWidth, 20, I18n.format("mw.config.map.ctgy.position.topleft")));
        this.buttonList.add(new GuiButtonExt(3001, this.width / 2 - buttonWidthHalf1 + topLeftWidth + 5, buttonHeigth1, centerTopWidth, 20, I18n.format("mw.config.map.ctgy.position.centertop")));
        this.buttonList.add(new GuiButtonExt(3002, this.width / 2 - buttonWidthHalf1 + topLeftWidth + 5 + centerTopWidth + 5, buttonHeigth1, topRightWidth, 20, I18n.format("mw.config.map.ctgy.position.topright")));
        this.buttonList.add(new GuiButtonExt(3010, this.width / 2 - buttonWidthHalf2, buttonHeigth2, centerLeft, 20, I18n.format("mw.config.map.ctgy.position.centerleft")));
        this.buttonList.add(new GuiButtonExt(3011, this.width / 2 - buttonWidthHalf2 + centerLeft + 5, buttonHeigth2, centerWidth, 20, I18n.format("mw.config.map.ctgy.position.center")));
        this.buttonList.add(new GuiButtonExt(3012, this.width / 2 - buttonWidthHalf2 + centerLeft + 5 + centerWidth + 5, buttonHeigth2, centerRight, 20, I18n.format("mw.config.map.ctgy.position.centerright")));
        this.buttonList.add(new GuiButtonExt(3020, this.width / 2 - buttonWidthHalf3, buttonHeigth3, bottomLeftWidth, 20, I18n.format("mw.config.map.ctgy.position.botleft")));
        this.buttonList.add(new GuiButtonExt(3021, this.width / 2 - buttonWidthHalf3 + bottomLeftWidth + 5, buttonHeigth3, centerBottomWidth, 20, I18n.format("mw.config.map.ctgy.position.centerbottom")));
        this.buttonList.add(new GuiButtonExt(3022, this.width / 2 - buttonWidthHalf3 + bottomLeftWidth + 5 + centerBottomWidth + 5, buttonHeigth3, bottomRightWidth, 20, I18n.format("mw.config.map.ctgy.position.botright")));
        this.updateParrentSettings();
    }

    protected void actionPerformed(GuiButton button) {
        double bottomOffset = 0.0D;
        if (!this.mapMode.getConfig().biomeMode.equals(MapModeConfig.coordsModeStringArray[0])) {
            bottomOffset = bottomOffset + (double) this.mc.fontRenderer.FONT_HEIGHT + 3.0D;
        }

        if (!this.mapMode.getConfig().biomeMode.equals(MapModeConfig.coordsModeStringArray[0])) {
            bottomOffset = bottomOffset + (double) this.mc.fontRenderer.FONT_HEIGHT + 3.0D;
        }

        bottomOffset = bottomOffset / (double) this.height * 100.0D;
        double SmallMarginY = 10.0D / (double) (this.height - this.mapMode.getH()) * 100.0D;
        double SmallMarginX = 10.0D / (double) (this.width - this.mapMode.getW()) * 100.0D;
        double LargeMarginBottom = 40.0D / (double) (this.height - this.mapMode.getH()) * 100.0D;
        bottomOffset = bottomOffset < SmallMarginY ? SmallMarginY : bottomOffset;
        if (button.id == 3000) {
            this.updateMap(new Double(SmallMarginX, SmallMarginY));
        } else if (button.id == 3001) {
            this.updateMap(new Double(50.0D, SmallMarginY));
        } else if (button.id == 3002) {
            this.updateMap(new Double(100.0D - SmallMarginX, SmallMarginY));
        } else if (button.id == 3010) {
            this.updateMap(new Double(SmallMarginX, 50.0D));
        } else if (button.id == 3011) {
            this.updateMap(new Double(50.0D, 50.0D));
        } else if (button.id == 3012) {
            this.updateMap(new Double(100.0D - SmallMarginX, 50.0D));
        } else if (button.id == 3020) {
            this.updateMap(new Double(SmallMarginX, 100.0D - bottomOffset));
        } else if (button.id == 3021) {
            this.updateMap(new Double(50.0D, 100.0D - LargeMarginBottom));
        } else if (button.id == 3022) {
            this.updateMap(new Double(100.0D - SmallMarginX, 100.0D - bottomOffset));
        } else {
            super.actionPerformed(button);
        }

    }

    public void mouseClicked(int x, int y, int mouseEvent) throws IOException {
        if (mouseEvent == 1 && this.mapMode.posWithin(x, y)) {
            this.DraggingMap = Boolean.TRUE;
        } else {
            super.mouseClicked(x, y, mouseEvent);
        }

    }

    protected void mouseClickMove(int x, int y, int mouseEvent, long timeSinceLastClick) {
        if (this.DraggingMap) {
            this.updateMap(this.mapMode.getNewPosPoint(x, y));
        } else {
            super.mouseClickMove(x, y, mouseEvent, timeSinceLastClick);
        }

    }

    public void mouseReleased(int x, int y, int mouseEvent) {
        if (this.DraggingMap) {
            this.DraggingMap = Boolean.FALSE;
        } else {
            super.mouseReleased(x, y, mouseEvent);
        }

    }

    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        super.drawScreen(mouseX, mouseY, partialTicks);
        this.updateButtonValues();
        this.map.drawDummy();
    }

    private void updateButtonValues() {

        for (IConfigEntry entry : this.entryList.listEntries) {
            if ("xPos".equals(entry.getName())) {
                this.dummyMapConfig.xPos = (java.lang.Double) entry.getCurrentValue();
            } else if ("yPos".equals(entry.getName())) {
                this.dummyMapConfig.yPos = (java.lang.Double) entry.getCurrentValue();
            } else if ("heightPercent".equals(entry.getName())) {
                this.dummyMapConfig.heightPercent = (java.lang.Double) entry.getCurrentValue();
            } else if ("widthPercent".equals(entry.getName())) {
                this.dummyMapConfig.widthPercent = (java.lang.Double) entry.getCurrentValue();
                ((ModGuiConfig.ModNumberSliderEntry) entry).setEnabled(!this.mapMode.getConfig().circular);
            }
        }

    }

    private void updateParrentSettings() {
        if (this.parentScreen != null && this.parentScreen instanceof GuiConfig) {
            GuiConfig parrent = (GuiConfig) this.parentScreen;
            if (parrent.entryList != null && parrent.entryList.listEntries != null) {
                Iterator var2 = parrent.entryList.listEntries.iterator();

                while (var2.hasNext()) {
                    IConfigEntry entry = (IConfigEntry) var2.next();
                    if ("circular".equals(entry.getName())) {
                        this.dummyMapConfig.circular = (Boolean) entry.getCurrentValue();
                    } else if ("coordsMode".equals(entry.getName())) {
                        this.dummyMapConfig.coordsMode = (String) entry.getCurrentValue();
                    } else if ("borderMode".equals(entry.getName())) {
                        this.dummyMapConfig.borderMode = (Boolean) entry.getCurrentValue();
                    } else if ("playerArrowSize".equals(entry.getName())) {
                        this.dummyMapConfig.playerArrowSize = Integer.parseInt((String) entry.getCurrentValue());
                    } else if ("biomeMode".equals(entry.getName())) {
                        this.dummyMapConfig.biomeMode = (String) entry.getCurrentValue();
                    }
                }
            }
        }

    }

    private void updateMap(Double point) {

        for (IConfigEntry entry : this.entryList.listEntries) {
            if (entry instanceof ModGuiConfig.ModNumberSliderEntry) {
                if ("xPos".equals(entry.getName())) {
                    ((ModGuiConfig.ModNumberSliderEntry) entry).setValue(point.getX());
                } else if ("yPos".equals(entry.getName())) {
                    ((ModGuiConfig.ModNumberSliderEntry) entry).setValue(point.getY());
                }
            }
        }

    }

    public static class MapPosConfigEntry extends CategoryEntry {
        public MapPosConfigEntry(GuiConfig owningScreen, GuiConfigEntries owningEntryList, IConfigElement prop) {
            super(owningScreen, owningEntryList, prop);
        }

        protected GuiScreen buildChildScreen() {
            String qualifiedName = this.configElement.getQualifiedName();
            String config = qualifiedName.substring(0, qualifiedName.indexOf(".")).replace(".", "");
            return new ModGuiConfigHUD(this.owningScreen, this.getConfigElement().getChildElements(), this.owningScreen.modID, null, this.configElement.requiresWorldRestart() || this.owningScreen.allRequireWorldRestart, this.configElement.requiresMcRestart() || this.owningScreen.allRequireMcRestart, this.owningScreen.title, config);
        }
    }
}
